package com.me.beanLife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class UserInfoBean implements InitializingBean, BeanNameAware, DisposableBean, ApplicationContextAware {

    @Value("${user.id:1001}")
    private Integer id;

    @Value("${activemq.broker.username:Tomcat}")
    private String name;

    public UserInfoBean (){
        System.out.println("2. 调用构造方法");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("6. 调用 BeanNameAware#setBeanName() 方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("12. 调用 DisposableBean#destroy() 方法");
    }

    public void initMethod() {
        System.out.println("10. 调用 init-method 方法");
    }

    public void destroyMethod() {
        System.out.println("13. 调用 destroy-method 方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("9. 调用 InitializingBean#afterPropertiesSet() 方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("7. 获取 ApplicationContext 上下文信息");
        UserInfoBean userBean = (UserInfoBean) applicationContext.getBean("userInfoBean");
        System.out.println(userBean);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        System.out.println("5. 属性注入 userId");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("5. 属性注入 name");
    }

    @Override
    public String toString() {
        return "\tUserInfoBean: {userId='" + id +"', name='" + name + "'}";
    }

}
